From f187578d1fe036572ae213905b7c4b214f69ebc6 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 10 Aug 2014 21:27:17 -0700 Subject: [PATCH] Disable doctests separately from tests This adds a `doctest = false` option to the `Cargo.toml` config for a target to disable doc tests for the target. Notably `test = false` does not disable this as it is separately toggleable. Closes #353 --- src/cargo/core/manifest.rs | 16 +++++++++--- src/cargo/ops/cargo_test.rs | 2 +- src/cargo/util/toml.rs | 52 ++++++++++++++++++------------------- tests/test_cargo_test.rs | 14 +++++----- 4 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index f31d322c9..c733f8962 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -109,6 +109,7 @@ pub struct Profile { opt_level: uint, debug: bool, test: bool, + doctest: bool, doc: bool, dest: Option, plugin: bool, @@ -124,6 +125,7 @@ impl Profile { doc: false, dest: None, plugin: false, + doctest: false, } } @@ -132,10 +134,7 @@ impl Profile { env: "compile".to_string(), // run in the default environment only opt_level: 0, debug: true, - test: false, // whether or not to pass --test - dest: None, - plugin: false, - doc: false, + .. Profile::default() } } @@ -189,6 +188,10 @@ impl Profile { self.test } + pub fn is_doctest(&self) -> bool { + self.doctest + } + pub fn is_plugin(&self) -> bool { self.plugin } @@ -224,6 +227,11 @@ impl Profile { self } + pub fn doctest(mut self, doctest: bool) -> Profile { + self.doctest = doctest; + self + } + pub fn doc(mut self, doc: bool) -> Profile { self.doc = doc; self diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index 695bd09c6..e4c9bb7d3 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -35,7 +35,7 @@ pub fn run_tests(manifest_path: &Path, } let mut libs = package.get_targets().iter().filter_map(|target| { - if !target.get_profile().is_test() || !target.is_lib() { + if !target.get_profile().is_doctest() || !target.is_lib() { return None } Some((target.get_src_path(), target.get_name())) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index d160fef2a..20aba7688 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -229,11 +229,8 @@ fn inferred_lib_target(name: &str, layout: &Layout) -> Vec { layout.lib.as_ref().map(|lib| { vec![TomlTarget { name: name.to_string(), - crate_type: None, path: Some(TomlPath(lib.clone())), - test: None, - plugin: None, - doc: None, + .. TomlTarget::new() }] }).unwrap_or(Vec::new()) } @@ -250,11 +247,8 @@ fn inferred_bin_targets(name: &str, layout: &Layout) -> Vec { name.map(|name| { TomlTarget { name: name, - crate_type: None, path: Some(TomlPath(bin.clone())), - test: None, - plugin: None, - doc: None, + .. TomlTarget::new() } }) }).collect() @@ -265,11 +259,8 @@ fn inferred_example_targets(layout: &Layout) -> Vec { ex.filestem_str().map(|name| { TomlTarget { name: name.to_string(), - crate_type: None, path: Some(TomlPath(ex.clone())), - test: None, - plugin: None, - doc: None, + .. TomlTarget::new() } }) }).collect() @@ -280,11 +271,8 @@ fn inferred_test_targets(layout: &Layout) -> Vec { ex.filestem_str().map(|name| { TomlTarget { name: name.to_string(), - crate_type: None, path: Some(TomlPath(ex.clone())), - test: None, - plugin: None, - doc: None, + .. TomlTarget::new() } }) }).collect() @@ -314,12 +302,8 @@ impl TomlManifest { self.lib.get_ref().iter().map(|t| { if layout.lib.is_some() && t.path.is_none() { TomlTarget { - name: t.name.clone(), - crate_type: t.crate_type.clone(), path: layout.lib.as_ref().map(|p| TomlPath(p.clone())), - test: t.test, - plugin: t.plugin, - doc: t.doc, + .. t.clone() } } else { t.clone() @@ -335,12 +319,8 @@ impl TomlManifest { self.bin.get_ref().iter().map(|t| { if bin.is_some() && t.path.is_none() { TomlTarget { - name: t.name.clone(), - crate_type: t.crate_type.clone(), path: bin.as_ref().map(|&p| TomlPath(p.clone())), - test: t.test, - plugin: None, - doc: t.doc, + .. t.clone() } } else { t.clone() @@ -462,6 +442,7 @@ struct TomlTarget { crate_type: Option>, path: Option, test: Option, + doctest: Option, doc: Option, plugin: Option, } @@ -472,6 +453,20 @@ enum TomlPath { TomlPath(Path), } +impl TomlTarget { + fn new() -> TomlTarget { + TomlTarget { + name: String::new(), + crate_type: None, + path: None, + test: None, + doctest: None, + doc: None, + plugin: None, + } + } +} + impl TomlPath { fn to_path(&self) -> Path { match *self { @@ -508,8 +503,11 @@ fn normalize(libs: &[TomlLibTarget], Some(false) => {} } + let doctest = target.doctest.unwrap_or(true); match target.doc { - Some(true) | None => ret.push(Profile::default_doc()), + Some(true) | None => { + ret.push(Profile::default_doc().doctest(doctest)); + } Some(false) => {} } diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 56e807194..2fb1b9772 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -223,6 +223,10 @@ test!(test_with_deep_lib_dep { [dependencies.foo] path = "../foo" + + [[lib]] + name = "bar" + doctest = false "#) .file("src/lib.rs", " extern crate foo; @@ -256,16 +260,9 @@ test!(test_with_deep_lib_dep { running 1 test test bar_test ... ok -test result: ok. 1 passed; 0 failed; 0 ignored; 0 measure - -{doctest} bar - -running 0 tests - -test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured\n\n\ +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n\ ", compiling = COMPILING, running = RUNNING, - doctest = DOCTEST, dir = p.url()).as_slice())); }) @@ -576,6 +573,7 @@ test!(lib_with_standard_name2 { [[lib]] name = "syntax" test = false + doctest = false "#) .file("src/lib.rs", " pub fn foo() {} -- 2.30.2